Completed
Push — master ( ca4e3a...09cec3 )
by Maxence
02:39 queued 10s
created

fts_admin_settings.updateEnabledSubProviders   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 16
rs 9.4285

1 Function

Rating   Name   Duplication   Size   Complexity  
A 0 14 3
1
/*
2
 * FullTextSearch - Full text search framework for Nextcloud
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2018
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: fts_admin_elements */
28
29
/** @namespace result.index_chunk */
30
/** @namespace result.platforms_all */
31
/** @namespace result.search_platform */
32
/** @namespace result.providers_all */
33
34
35
36
37
var fts_admin_settings = {
38
39
	config: null,
40
41
	refreshSettingPage: function () {
42
43
		$.ajax({
44
			method: 'GET',
45
			url: OC.generateUrl('/apps/fulltextsearch/admin/settings')
46
		}).done(function (res) {
47
			fts_admin_settings.updateSettingPage(res);
48
		});
49
50
	},
51
52
53
	updateSettingPage: function (result) {
54
55
		fts_admin_elements.fts_navigation.prop('checked', (result.app_navigation === '1'));
56
57
		fts_admin_settings.updateSettingPagePlatforms(result);
58
		fts_admin_settings.updateSettingPageChunkSize(result);
59
60
		fts_admin_settings.updateCurrentPlatform(result);
61
		fts_admin_settings.updateEnabledProviders(result);
62
63
		fts_admin_settings.tagSettingsAsSaved(fts_admin_elements.fts_div);
64
	},
65
66
67
	updateSettingPagePlatforms: function (result) {
68
		fts_admin_elements.fts_platforms.empty();
69
		fts_admin_elements.fts_platforms.append($('<option>', {
70
			value: '',
71
			text: ''
72
		}));
73
74
		var platforms = result.platforms_all;
75
		var classes = Object.keys(platforms);
76
		for (var i = 0; i < classes.length; i++) {
77
			var platformClass = classes[i];
78
			fts_admin_elements.fts_platforms.append($('<option>', {
79
				value: platformClass,
80
				selected: (result.search_platform === platformClass),
81
				text: platforms[platformClass].name
82
			}));
83
			$('#' + platforms[platformClass].id).fadeTo(300, 0, function () {
0 ignored issues
show
Bug introduced by
It is generally not recommended to make functions within a loop.

While making functions in a loop will not lead to any runtime error, the code might not behave as you expect as the variables in the scope are not imported by value, but by reference. Let’s take a look at an example:

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(function() {
        alert(i);
    });
}

funcs[0](); // alert(10);
funcs[1](); // alert(10);
/// ...
funcs[9](); // alert(10);

If you would instead like to bind the function inside the loop to the value of the variable during that specific iteration, you can create the function from another function:

var createFunc = function(i) {
    return function() {
        alert(i);
    };
};

var funcs = [];
for (var i=0; i<10; i++) {
    funcs.push(createFunc(i));
}

funcs[0](); // alert(0)
funcs[1](); // alert(1)
// ...
funcs[9](); // alert(9)
Loading history...
84
				$(this).hide();
85
			});
86
		}
87
88
		fts_admin_elements.fts_platforms.fadeTo(300, 1);
89
	},
90
91
92
	updateSettingPageChunkSize: function (result) {
93
		fts_admin_elements.fts_chunkSize.val(result.index_chunk);
94
		fts_admin_elements.fts_chunkSize.fadeTo(300, 1);
95
	},
96
97
98
	updateCurrentPlatform: function (result) {
99
		if (result.search_platform.length === 0) {
100
			return;
101
		}
102
103
		$('#' + result.platforms_all[result.search_platform].id).stop().show().fadeTo(300, 1);
104
	},
105
106
107
	updateEnabledProviders: function (result) {
108
109
		var providers = result.providers_all;
110
		var providerIds = Object.keys(providers);
111
		for (var i = 0; i < providerIds.length; i++) {
112
			$('#' + providerIds[i]).stop().fadeTo(300, 0);
113
		}
114
115
		$('.subprovider').stop().fadeTo(300, 0);
116
117
		// we only check that a search_platform is valid. we don't manage a list of enabled provider as
118
		// of right now
119
		if (result.search_platform.length === 0) {
120
			return;
121
		}
122
123
		for (i = 0; i < providerIds.length; i++) {
124
			$('#' + providerIds[i]).stop().fadeTo(300, 1);
125
		}
126
	},
127
128
129
	updateEnabledSubProviders: function () {
130
		$('body').find('.subprovider').each(function () {
131
			var top = $(this).attr('id').split('-', 2);
132
133
			if (top.length < 2) {
134
				return;
135
			}
136
137
			var topOption = top[0];
138
			if ($('#' + topOption).is(':checked')) {
139
				$(this).stop().fadeTo(300, 1).slideDown();
140
			} else {
141
				$(this).stop().fadeTo(300, 0).slideUp();
142
			}
143
		});
144
	},
145
146
147
	tagSettingsAsNotSaved: function (div) {
148
		div.animate({
149
			'backgroundColor': 'rgba(255, 180, 0, 0.18)'
150
		}, 300);
151
	},
152
153
154
	tagSettingsAsSaved: function (div) {
155
		div.find('INPUT').animate({'backgroundColor': 'rgba(255, 255, 255, 0.18)'}, 300);
156
		div.find('SELECT').animate({'backgroundColor': '#fff'}, 300);
157
158
		fts_admin_settings.updateEnabledSubProviders();
159
	},
160
161
162
	saveSettings: function () {
163
164
		var data = {
165
			app_navigation: (fts_admin_elements.fts_navigation.is(':checked')) ? 1 : 0,
166
			search_platform: fts_admin_elements.fts_platforms.val(),
167
			index_chunk: fts_admin_elements.fts_chunkSize.val()
168
		};
169
170
		$.ajax({
171
			method: 'POST',
172
			url: OC.generateUrl('/apps/fulltextsearch/admin/settings'),
173
			data: {
174
				data: data
175
			}
176
		}).done(function (res) {
177
			fts_admin_settings.updateSettingPage(res);
178
		});
179
180
	}
181
182
183
};
184